package o;

import androidx.annotation.MainThread;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map.Entry;
import o.kp.e;

public class kx extends kp {
    private boolean ʻ = false;
    private ArrayList<o.kp.c> ʽ = new ArrayList();
    private o.kp.c ˊ;
    private int ˋ = 0;
    private final WeakReference<kv> ˎ;
    private dj<kt, c> ॱ = new dj();
    private boolean ᐝ = false;

    static class c {
        o.kp.c ˊ;
        kq ˏ;

        c(kt ktVar, o.kp.c cVar) {
            this.ˏ = ku.ॱ(ktVar);
            this.ˊ = cVar;
        }

        void ˊ(kv kvVar, e eVar) {
            o.kp.c ˎ = kx.ˎ(eVar);
            this.ˊ = kx.ˊ(this.ˊ, ˎ);
            this.ˏ.ˊ(kvVar, eVar);
            this.ˊ = ˎ;
        }
    }

    public kx(@NonNull kv kvVar) {
        this.ˎ = new WeakReference(kvVar);
        this.ˊ = o.kp.c.INITIALIZED;
    }

    @MainThread
    @Deprecated
    public void ˋ(@NonNull o.kp.c cVar) {
        ˊ(cVar);
    }

    @MainThread
    public void ˊ(@NonNull o.kp.c cVar) {
        ˎ(cVar);
    }

    public void ˊ(@NonNull e eVar) {
        ˎ(ˎ(eVar));
    }

    private void ˎ(o.kp.c cVar) {
        if (this.ˊ != cVar) {
            this.ˊ = cVar;
            if (this.ʻ || this.ˋ != 0) {
                this.ᐝ = true;
                return;
            }
            this.ʻ = true;
            ॱ();
            this.ʻ = false;
        }
    }

    private boolean ˏ() {
        if (this.ॱ.ˏ() == 0) {
            return true;
        }
        o.kp.c cVar = ((c) this.ॱ.ˋ().getValue()).ˊ;
        o.kp.c cVar2 = ((c) this.ॱ.ˎ().getValue()).ˊ;
        boolean z = cVar == cVar2 && this.ˊ == cVar2;
        return z;
    }

    private o.kp.c ˊ(kt ktVar) {
        o.kp.c cVar;
        o.kp.c cVar2;
        Entry ˋ = this.ॱ.ˋ(ktVar);
        if (ˋ != null) {
            cVar = ((c) ˋ.getValue()).ˊ;
        } else {
            cVar = null;
        }
        if (this.ʽ.isEmpty()) {
            cVar2 = null;
        } else {
            cVar2 = (o.kp.c) this.ʽ.get(this.ʽ.size() - 1);
        }
        return ˊ(ˊ(this.ˊ, cVar), cVar2);
    }

    public void ॱ(@NonNull kt ktVar) {
        c cVar = new c(ktVar, this.ˊ == o.kp.c.DESTROYED ? o.kp.c.DESTROYED : o.kp.c.INITIALIZED);
        if (((c) this.ॱ.ˏ(ktVar, cVar)) == null) {
            kv kvVar = (kv) this.ˎ.get();
            if (kvVar != null) {
                Object obj = (this.ˋ != 0 || this.ʻ) ? 1 : null;
                Enum ˊ = ˊ(ktVar);
                this.ˋ++;
                while (cVar.ˊ.compareTo(ˊ) < 0 && this.ॱ.ˏ(ktVar)) {
                    ˏ(cVar.ˊ);
                    cVar.ˊ(kvVar, ʻ(cVar.ˊ));
                    ˊ();
                    ˊ = ˊ(ktVar);
                }
                if (obj == null) {
                    ॱ();
                }
                this.ˋ--;
            }
        }
    }

    private void ˊ() {
        this.ʽ.remove(this.ʽ.size() - 1);
    }

    private void ˏ(o.kp.c cVar) {
        this.ʽ.add(cVar);
    }

    public void ˋ(@NonNull kt ktVar) {
        this.ॱ.ˊ(ktVar);
    }

    @NonNull
    public o.kp.c ˋ() {
        return this.ˊ;
    }

    static o.kp.c ˎ(e eVar) {
        switch (eVar) {
            case ON_CREATE:
            case ON_STOP:
                return o.kp.c.CREATED;
            case ON_START:
            case ON_PAUSE:
                return o.kp.c.STARTED;
            case ON_RESUME:
                return o.kp.c.RESUMED;
            case ON_DESTROY:
                return o.kp.c.DESTROYED;
            default:
                throw new IllegalArgumentException("Unexpected event value " + eVar);
        }
    }

    private static e ॱ(o.kp.c cVar) {
        switch (cVar) {
            case INITIALIZED:
                throw new IllegalArgumentException();
            case CREATED:
                return e.ON_DESTROY;
            case STARTED:
                return e.ON_STOP;
            case RESUMED:
                return e.ON_PAUSE;
            case DESTROYED:
                throw new IllegalArgumentException();
            default:
                throw new IllegalArgumentException("Unexpected state value " + cVar);
        }
    }

    private static e ʻ(o.kp.c cVar) {
        switch (cVar) {
            case INITIALIZED:
            case DESTROYED:
                return e.ON_CREATE;
            case CREATED:
                return e.ON_START;
            case STARTED:
                return e.ON_RESUME;
            case RESUMED:
                throw new IllegalArgumentException();
            default:
                throw new IllegalArgumentException("Unexpected state value " + cVar);
        }
    }

    private void ˎ(kv kvVar) {
        Iterator ˊ = this.ॱ.ˊ();
        while (ˊ.hasNext() && !this.ᐝ) {
            Entry entry = (Entry) ˊ.next();
            c cVar = (c) entry.getValue();
            while (cVar.ˊ.compareTo(this.ˊ) < 0 && !this.ᐝ && this.ॱ.ˏ(entry.getKey())) {
                ˏ(cVar.ˊ);
                cVar.ˊ(kvVar, ʻ(cVar.ˊ));
                ˊ();
            }
        }
    }

    private void ॱ(kv kvVar) {
        Iterator ॱ = this.ॱ.ॱ();
        while (ॱ.hasNext() && !this.ᐝ) {
            Entry entry = (Entry) ॱ.next();
            c cVar = (c) entry.getValue();
            while (cVar.ˊ.compareTo(this.ˊ) > 0 && !this.ᐝ && this.ॱ.ˏ(entry.getKey())) {
                e ॱ2 = ॱ(cVar.ˊ);
                ˏ(ˎ(ॱ2));
                cVar.ˊ(kvVar, ॱ2);
                ˊ();
            }
        }
    }

    private void ॱ() {
        kv kvVar = (kv) this.ˎ.get();
        if (kvVar == null) {
            throw new IllegalStateException("LifecycleOwner of this LifecycleRegistry is alreadygarbage collected. It is too late to change lifecycle state.");
        }
        while (!ˏ()) {
            this.ᐝ = false;
            if (this.ˊ.compareTo(((c) this.ॱ.ˋ().getValue()).ˊ) < 0) {
                ॱ(kvVar);
            }
            Entry ˎ = this.ॱ.ˎ();
            if (!(this.ᐝ || ˎ == null || this.ˊ.compareTo(((c) ˎ.getValue()).ˊ) <= 0)) {
                ˎ(kvVar);
            }
        }
        this.ᐝ = false;
    }

    static o.kp.c ˊ(@NonNull o.kp.c cVar, @Nullable o.kp.c cVar2) {
        return (cVar2 == null || cVar2.compareTo(cVar) >= 0) ? cVar : cVar2;
    }
}
